package org.leetcode.hard.leetcode42;

public class Solution
{
    public int trap(int[] height)
    {

        int sum =0;

        for (int i = 1; i <height.length-1; i++) {

            int max_left = 0;

            //找到当前数字左边最大数
            for (int j = 0; j <i; j++) {
                if (height[j]>max_left)
                {
                    max_left=height[j];
                }
            }


            int max_right = 0;
            //找到当前数字右边最大数
            for (int j = i+1; j <height.length; j++) {
                if (height[j]>max_right)
                {
                    max_right=height[j];
                }
            }

            //找到左边最大数和右边最大数的最小值
            int min =Math.min(max_left,max_right);

            //根据木桶效应，只有当前数比两边还要小，当前列的上方才会有积水
            if (min>height[i])
            {
                sum=sum+(min-height[i]);
            }

        }

        return sum;
    }
}
